package com.lsa.southwind.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsa.southwind.entity.Dormitory;
import com.lsa.southwind.entity.Student;
import com.lsa.southwind.form.SearchForm;
import com.lsa.southwind.form.StudentForm;
import com.lsa.southwind.mapper.DormitoryMapper;
import com.lsa.southwind.mapper.StudentMapper;
import com.lsa.southwind.service.StudentService;
import com.lsa.southwind.util.CommonUtil;
import com.lsa.southwind.vo.PageVo;
import com.lsa.southwind.vo.StudentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2023-02-25
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private DormitoryMapper dormitoryMapper;

    @Override
    public Boolean saveStudent(Student student) {
        //添加成功,修改入住的宿舍的人数数量
        //1.先查看要入住的宿舍的数量，通过学生宿舍id去查询该宿舍的全部信息
        Dormitory dormitory = this.dormitoryMapper.selectById(student.getDormitoryId());
        //2.判断要入住的宿舍的人数
        if (dormitory.getAvailable() == 0) {
            return false;
        } else {
            //3.如果不等于0就进行人数更改
            dormitory.setAvailable(dormitory.getAvailable() - 1);
            int updateById = this.dormitoryMapper.updateById(dormitory);
            if (updateById != 1) {
                return false;
            }
        }
        //4.添加学生
        student.setCreateDate(CommonUtil.createDate());
        student.setState("入住");
        int insert = this.studentMapper.insert(student);
        if (insert != 1) {
            return false;
        } else {
            return true;
        }

    }

    /*学生页面查询*/
    @Override
    public PageVo list(Integer page, Integer size) {
        /*分页查询 page页数,size大小*/
        Page<Student> studentListPage = new Page<>(page, size);
        Page<Student> resultPage = this.studentMapper.selectPage(studentListPage, null);
        /*因为前端要的是宿舍名，而我们后端查询出来的是宿舍的id所有我们要把他转化一下*/
        List<Student> studentList = resultPage.getRecords();
        //将Vo转换
        List<StudentVo> studentVoList = new ArrayList<>();
        for (Student student : studentList) {
            /*前端需要的实体类*/
            StudentVo studentVo = new StudentVo();
            /*属性的复制 左边为被复制的属性，右边是目标属性*/
            BeanUtils.copyProperties(student, studentVo);
            /*现在根据学生的宿舍id查出宿舍名*/
            Dormitory dormitory = this.dormitoryMapper.selectById(student.getDormitoryId());
            studentVo.setDormitoryName(dormitory.getName());
            studentVoList.add(studentVo);

        }
        PageVo pageVo = new PageVo();
        /*获取总条数*/
        pageVo.setTotal(resultPage.getTotal());
        /*存入查出的数据*/
        System.out.println("查询出的数据:" + studentVoList);
        pageVo.setData(studentVoList);
        return pageVo;
    }

    /*学生页面搜索*/
    @Override
    public PageVo search(SearchForm searchForm) {
        /*进行分页操作*/
        Page<Student> StudentSearchPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<Student> studentPage = null;
        /*如果搜索框的值为null就选择查询全部*/
        if (searchForm.getValue().equals("")) {
            studentPage = this.studentMapper.selectPage(StudentSearchPage, null);

        } else {
            /*否则就模糊查询*/
            QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
            /*字段名,值*/
            queryWrapper.like(searchForm.getKey(), searchForm.getValue());
            studentPage = this.studentMapper.selectPage(StudentSearchPage, queryWrapper);
        }
        /*因为前端要的是宿舍名，而我们后端查询出来的是宿舍的id所有我们要把他转化一下*/
        List<Student> studentList = studentPage.getRecords();
        //将Vo转换
        List<StudentVo> studentVoList = new ArrayList<>();
        for (Student student : studentList) {
            /*前端需要的实体类*/
            StudentVo studentVo = new StudentVo();
            /*属性的复制 左边为被复制的属性，右边是目标属性*/
            BeanUtils.copyProperties(student, studentVo);
            /*现在根据学生的宿舍id查出宿舍名*/
            Dormitory dormitory = this.dormitoryMapper.selectById(student.getDormitoryId());
            studentVo.setDormitoryName(dormitory.getName());
            studentVoList.add(studentVo);

        }
        PageVo pageVo = new PageVo();
        /*获取总条数*/
        pageVo.setTotal(studentPage.getTotal());
        /*存入查出的数据*/
        System.out.println("查询出的数据:" + studentVoList);
        pageVo.setData(studentVoList);
        return pageVo;
    }

    /*学生的修改*/
    @Override
    public Boolean update(StudentForm studentForm) {
        //1.更新学生
        Student student = new Student();
        BeanUtils.copyProperties(studentForm, student);
        /*通过新宿舍id查询出全部宿舍信息*/
        Dormitory dormitoryNew = this.dormitoryMapper.selectById(student.getDormitoryId());
        if (dormitoryNew.getAvailable() == 0) {
            return false;
        } else {
            int updateById = this.studentMapper.updateById(student);
            if (updateById != 1) {
                return false;
            } else {
                /*当提交老宿舍id与新的不一样时说明他修改了*/
                if (!studentForm.getDormitoryId().equals(studentForm.getOldDormitoryId())) {
                    //老宿舍+1
                    try {
                        this.dormitoryMapper.addAvailable(studentForm.getOldDormitoryId());
                        //新宿舍-1
                        this.dormitoryMapper.subAvailable(studentForm.getDormitoryId());
                    } catch (Exception e) {
                        return false;
                    }
                }
                return true;
            }
        }
    }

    /*删除学生*/
    @Override
    public Boolean deleteById(Integer id) {
        //首先要修改学生的
        /*1.通过学生id查出学生信息*/
        Student student = this.studentMapper.selectById(id);

        try {
            /*2.通过学生的宿舍id去查询他住在几号宿舍*/
            Dormitory dormitory = this.dormitoryMapper.selectById(student.getDormitoryId());
            if (dormitory.getType() > dormitory.getAvailable()) {
                /*根据查询出宿舍，进行人数添加*/
                this.dormitoryMapper.addAvailable(dormitory.getId());
            }
        } catch (Exception e) {
            return false;
        }
        //删除学生数据
        int delete = this.studentMapper.deleteById(id);
        if (delete != 1) {
            return false;
        }
        return true;
    }


}
